package com.jianjun.project.system.notice;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.Method;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jianjun.common.constant.HttpStatus;
import com.jianjun.common.util.SecurityUtils;
import com.jianjun.framework.web.domain.AjaxResult;
import com.jianjun.framework.web.interceptor.DataScope;
import com.jianjun.framework.web.page.TableDataInfo;
import com.jianjun.project.common.model.SysNotice;

/**
 * 公告 信息操作处理
 *
 * @author source
 */
@Path(value = "/system/notice")
public class SysNoticeController extends Controller {
    @Inject
    SysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @DataScope(permission = "system:notice:list")
    public void list() {
        SysNotice sysNotice = JsonKit.parse(getKv().toJson(), SysNotice.class);
        Page<SysNotice> page = noticeService.paginate(sysNotice);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotalRow());
        renderJson(rspData);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @DataScope(permission = "system:notice:query,system:notice:add,system:notice:edit,system:notice:remove")
    public void index() {
        String method = getRequest().getMethod();
        //查询
        if (Method.GET.name().equals(method)) {
            Long noticeId = getParaToLong();
            renderJson(AjaxResult.success(noticeService.selectNoticeById(noticeId)));
        }
        //新增
        if (Method.POST.name().equals(method)) {
            SysNotice sysNotice = JsonKit.parse(getRawData(), SysNotice.class);
            sysNotice.setCreateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(noticeService.insertNotice(sysNotice)));
        }
        //修改
        if (Method.PUT.name().equals(method)) {
            SysNotice sysNotice = JsonKit.parse(getRawData(), SysNotice.class);
            sysNotice.setUpdateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(noticeService.updateNotice(sysNotice)));
        }
        //删除
        if (Method.DELETE.name().equals(method)) {
            renderJson(AjaxResult.ajax(noticeService.deleteNoticeByIds(Convert.toLongArray(getPara()))));
        }
    }
}
